#include "contactData.h"
contactData::contactData()
{
//	loadContactXml();
}
contactData::~contactData()
{
//	saveContactXml();
}
void contactData::saveContactXml()
{
	QFile file("contact.xml");
	if(file.exists())//如果文件存在则先删除后创建保存
		file.remove();
	if((myList.count() == 0) && (myListBlack.count() == 0))
	{
		file.close();
		return;//如果列表为空，则不需要保存
	}
	if(!file.open(QIODevice::ReadWrite))
		return;
	QTextStream out(&file);
	out.setCodec("UTF-8");
	QDomDocument doc;

	QDomProcessingInstruction instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
	/*写入头*/
	doc.appendChild(instruction);
	/*写入根结点*/
	QDomElement root = doc.createElement("Contacts");
	doc.appendChild(root);

	for(int i=0; i<myList.count(); i++)
	{
		/*写入people结点*/
		QString tag = QString("people%1").arg(i);
		QDomElement people = doc.createElement(tag);
		root.appendChild(people);

		/*写入姓名结点*/
		QDomElement name = doc.createElement("name");
		people.appendChild(name);
		QDomText name_txt = doc.createTextNode(myList[i].name);
		name.appendChild(name_txt);

		/*写入电话号码结点*/
		QDomElement number = doc.createElement("phoneNumNumber");
		people.appendChild(number);
		QDomText phoneNum_txt = doc.createTextNode(myList[i].phoneNum);
		number.appendChild(phoneNum_txt);

		/*写入黑名单结点*/
		QDomElement black = doc.createElement("black");
		people.appendChild(black);

		QDomText black_txt;
		if(myList[i].isBlack == true)
			black_txt = doc.createTextNode("true");
		else
			black_txt = doc.createTextNode("false");

		black.appendChild(black_txt);
	}
	for(int i=0; i<myListBlack.count(); i++)
	{
		/*写入people结点*/
		QString tag = QString("people%1").arg(i+myList.count());
		QDomElement people = doc.createElement(tag);
		root.appendChild(people);

		/*写入姓名结点*/
		QDomElement name = doc.createElement("name");
		people.appendChild(name);
		QDomText name_txt = doc.createTextNode(myListBlack[i].name);
		name.appendChild(name_txt);

		/*写入电话号码结点*/
		QDomElement number = doc.createElement("phoneNumNumber");
		people.appendChild(number);
		QDomText phoneNum_txt = doc.createTextNode(myListBlack[i].phoneNum);
		number.appendChild(phoneNum_txt);

		/*写入黑名单结点*/
		QDomElement black = doc.createElement("black");
		people.appendChild(black);

		QDomText black_txt;
		if(myListBlack[i].isBlack == true)
			black_txt = doc.createTextNode("true");
		else
			black_txt = doc.createTextNode("false");

		black.appendChild(black_txt);
	}

	doc.save(out,4,QDomNode::EncodingFromTextStream);//将准备好的xml文档写入文件中

	file.close();
}

void contactData::loadContactXml()
{
	qDebug("locad contact from contact.xml");
	QFile file("contact.xml");
	if(!file.exists()) return;//如果文件不存在，则不需要加载
	if(!file.open(QIODevice::ReadWrite))
	{
		qDebug("error while open contact.xml");
	}
	QDomDocument doc;
	QString errorStr;
	int errorLine;
	int errorColumn;
	if(!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
	{
		cout<<"error setContent"<<endl;
	}
	//QString people,name,phoneNum,black;
	myList.clear();
	myListBlack.clear();
	QDomNodeList list = doc.childNodes();
	for(int i=0; i<list.count();i++){
		QDomNodeList list_2 = list.at(i).childNodes();
		for(int j=0; j<list_2.count(); j++)
		{
			QDomNodeList list_3 = list_2.at(j).childNodes();

			contact t_element;
			t_element.name = list_3.at(0).toElement().text();
			t_element.phoneNum = list_3.at(1).toElement().text();

			QString blackStr = list_3.at(2).toElement().text();
			if(blackStr == QString("false"))
			{
				t_element.isBlack = false;
				myList.push_back(t_element);
			}
			else
			{
				t_element.isBlack = true;
				myListBlack.push_back(t_element);
			}
		}
	}
	file.close();
	//updateList();
}


void contactData::moveToBlack(int row)
{
	contact item = myList.at(row);
	myList.removeAt(row);
	myListBlack.push_back(item);
}

void contactData::moveFromBlack(int row)
{
	contact item = myListBlack.at(row);
	myListBlack.removeAt(row);
	myList.push_back(item);
}
QString contactData::getNameForNum(QString phoneNum)
{
	for(int i=0; i<myList.count(); i++)
	{
		if(myList[i].phoneNum == phoneNum)
		return myList[i].name;
	}
	for(int i=0; i<myListBlack.count(); i++)
	{
		if(myListBlack[i].phoneNum == phoneNum)
		return myListBlack[i].name;
	}
	return phoneNum;
}


